﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class MyAgentClientListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["SELECTED_AGENTID_IS_MY_DOWNLINE_LEAD"] != null)
                {
                    btnAddNew.Visible = false;
                }
            }
        }
        protected void lnkViewMap_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int Clientid = Convert.ToInt32(e.CommandArgument.ToString());

            if (companyid > 0 && Clientid > 0)
                ucModalViewMapControl.ShowMapDataByBillingClientID(companyid, Clientid);
        }
        protected void lnkViewClient_Click(object sender, CommandEventArgs e)
        {
            int Clientid = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("RSClientDetail.aspx?Clientid=" + Clientid);
        }
        protected void lnkViewAgent_Click(object sender, CommandEventArgs e)
        {
            int agentid = Convert.ToInt32(e.CommandArgument.ToString());
            Response.Redirect("RSAgentDetail.aspx?employeeid=" + agentid);
        }
        protected void btnSearchClients_Click(object sender, EventArgs e)
        {
            int companyid = hfCompanyId.Value == "" ? 0 : Convert.ToInt32(hfCompanyId.Value);
            int agentid = hfAgentId.Value == "" ? 0 : Convert.ToInt32(hfAgentId.Value);
            LoadAllClientWhoseAgentReportToMe(companyid, agentid);
        }

        public void LoadAllClientWhoseAgentReportToMe(int companyid, int reportsto)
        {
            hfCompanyId.Value = companyid.ToString();
            hfAgentId.Value = reportsto.ToString();

            SqlCommand myCommand;
            String sqlSELECT = "select c.*, e.employeeid, e.reportsto from rs_tblClient c inner join rs_tblEmployee e on c.agentid=e.EmployeeId";
            String sqlWHERE = " where c.CompanyID=@companyid and e.reportsto=@reportsto";

            List<string> paramlist = new List<string>();
            paramlist.Add("@companyid");
            paramlist.Add("@reportsto");

            if (txtAccountID.Text != "")
            {
                sqlWHERE = sqlWHERE + " and c.accountid=@accountid";
                paramlist.Add("@accountid");
            }
            else
            {
                if (txtFirstName.Text != "")
                {
                    sqlWHERE = sqlWHERE + " and (CHARINDEX(@firstname, c.firstname)>0)";
                    paramlist.Add("@firstname");
                }
                if (txtLastName.Text != "")
                {
                    sqlWHERE = sqlWHERE + " and (CHARINDEX(@lastname, c.lastname)>0)";
                    paramlist.Add("@lastname");
                }
            }

            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT + " " + sqlWHERE + "  ORDER BY c.LastName, c.FirstName ", myConnection);

            for (int i = 0; i < paramlist.Count(); i++)
            {
                if (paramlist[i] == "@companyid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@companyid";
                    param.Value = companyid;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@accountid")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@accountid";
                    param.Value = int.Parse(txtAccountID.Text);
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@reportsto")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.Int;
                    param.ParameterName = "@reportsto";
                    param.Value = reportsto;
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@firstname")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@firstname";
                    param.Value = txtFirstName.Text.Trim();
                    myCommand.Parameters.Add(param);
                }
                if (paramlist[i] == "@lastname")
                {
                    param = new SqlParameter();
                    param.SqlDbType = SqlDbType.NVarChar;
                    param.ParameterName = "@lastname";
                    param.Value = txtLastName.Text.Trim();
                    myCommand.Parameters.Add(param);
                }
            }

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];

            gvClients.DataSource = ds;
            gvClients.DataBind();
            lblClientCount.Text = ds.Tables[0].Rows.Count.ToString();
            myConnection.Close();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                ucModalCreateClientControl.ShowModal(userIdentity.RSCompanyId, userIdentity.RSEmployeeId);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);
        }
        
        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvClients_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvClients.PageIndex = e.NewPageIndex;
            gvClients.DataBind();
        }

        protected void gvClients_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvClients.DataSource = dataView;
                gvClients.DataBind();
            }
        }

    }
}